package org.aplombh.java.interview.newcoder;

import java.util.Scanner;

public class HJ101_输入整型数组和排序标识_对其元素按照升序或降序进行排序 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int[] ints = new int[n];
            for (int i = 0; i < n; i++) {
                ints[i] = in.nextInt();
            }
            int flag = in.nextInt();

//            mergeSort(ints, new int[n], 0, n - 1);
            quickSort(ints, 0, ints.length - 1);

            if (flag == 1) {
                int[] tmp = new int[n];
                for (int i = 0; i < n; i++) {
                    tmp[i] = ints[n - i - 1];
                }
                ints = tmp;
            }

            for (int i = 0; i < n; i++) {
                System.out.print(ints[i] + " ");
            }
        }
    }

    private static void mergeSort(int[] ints, int[] tmp, int l, int r) {
        if (l >= r) return;
        int mid = l + r >> 1;

        mergeSort(ints, tmp, l, mid);
        mergeSort(ints, tmp, mid + 1, r);

        int k = 0, i = l, j = mid + 1;

        while (i <= mid && j <= r) {
            if (ints[i] <= ints[j]) tmp[k++] = ints[i++];
            else tmp[k++] = ints[j++];
        }

        while (i <= mid) {
            tmp[k++] = ints[i++];
        }

        while (j <= r) {
            tmp[k++] = ints[j++];
        }

        for (i = l, k = 0; i <= r; i++, k++) ints[i] = tmp[k];
    }

    private static void quickSort(int[] ints, int l, int r) {
        if (l >= r) return;
        int x = ints[l + r >> 1], i = l - 1, j = r + 1;
        while (i < j) {
            while (ints[++i] < x) ;
            while (ints[--j] > x) ;

            if (i < j) {
                int tmp = ints[i];
                ints[i] = ints[j];
                ints[j] = tmp;
            }
        }

        quickSort(ints, l, j);
        quickSort(ints, j + 1, r);
    }
}